package com.cxh.rmsq.ui.activity.self

import android.graphics.Color
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivityJoinRecordBinding
import com.cxh.rmsq.databinding.ActivityRankListBinding
import com.cxh.rmsq.ui.BaseActivity
import com.cxh.rmsq.ui.fragment.self.JoinRecordFragment
import com.cxh.rmsq.ui.fragment.self.MineTrendsListFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

/**
 * 参与记录
 */
class JoinRecordActivity : BaseActivity() {
    private lateinit var binding: ActivityJoinRecordBinding
    private val tabs = arrayOf("进行中", "已完成")
    private val mFragments = ArrayList<Fragment>()
    private var mediator: TabLayoutMediator? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityJoinRecordBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.clToolbar.setPadding(0, 0, 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)

        binding.ivBack.setOnClickListener {
            finish()
        }
        mFragments.clear()

        mFragments.add(JoinRecordFragment.newInstance(0))
        mFragments.add(JoinRecordFragment.newInstance(1))


        //Adapter
        binding.viewPager.setAdapter(object :
            FragmentStateAdapter(supportFragmentManager, lifecycle) {
            override fun createFragment(position: Int): Fragment {
                //FragmentStateAdapter内部自己会管理已实例化的fragment对象。
                // 所以不需要考虑复用的问题
                return mFragments[position]
            }

            override fun getItemCount(): Int {
                return mFragments.size
            }
        })
        //viewPager 页面切换监听监听
        //viewPager 页面切换监听监听
        binding.viewPager.registerOnPageChangeCallback(changeCallback)
//        binding.viewPager.setUserInputEnabled(false);
        //        binding.viewPager.setUserInputEnabled(false);
        binding.viewPager.setOffscreenPageLimit(mFragments.size - 1)

        mediator = TabLayoutMediator(
            binding.tabCommunity, binding.viewPager
        ) { tab, position -> //这里可以自定义TabView
            val customView = tab.customView
            if (customView == null) {
                tab.setCustomView(R.layout.tab_item_camp)
            }
            val tab_text = tab.customView!!.findViewById<TextView>(R.id.tab_text)
            val tab_indicator =
                tab.customView!!.findViewById<ImageView>(R.id.tab_indicator)
            if (position < tabs.size) {
                tab_text.text = tabs[position]
            }
            tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
            tab_text.setTypeface(Typeface.DEFAULT_BOLD)
            tab_text.setTextColor(Color.parseColor("#FFDF83"))
            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator)
        }
        //要执行这一句才是真正将两者绑定起来
        mediator!!.attach()
    }

    private val changeCallback: ViewPager2.OnPageChangeCallback =
        object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                //可以来设置选中时tab的大小
                val tabCount: Int = binding.tabCommunity.getTabCount()
                for (i in 0 until tabCount) {
                    val tab: TabLayout.Tab = binding.tabCommunity.getTabAt(i)!!
                    if (tab != null) {
                        val customView = tab.customView
                        if (customView != null) {
                            val tab_text = tab.customView!!.findViewById<TextView>(R.id.tab_text)
                            val tab_indicator =
                                tab.customView!!.findViewById<ImageView>(R.id.tab_indicator)

                            if (tab.position == position) {
                                tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
                                tab_text.setTypeface(Typeface.DEFAULT_BOLD)
                                tab_indicator.visibility = View.VISIBLE
                                tab_text.setTextColor(Color.parseColor("#FFDF83"))
                            } else {
                                tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
                                tab_text.setTypeface(Typeface.DEFAULT)
                                tab_indicator.visibility = View.INVISIBLE
                                tab_text.setTextColor(Color.parseColor("#D7CBA7"))
                            }
                        }
                    }
                }
            }
        }
}